package zhtt.example.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;

/**
 * Created by zhaoq on   2024/8/3 15:15
 **/
@Configuration
@Slf4j
public class RedirectRouterConfig {

    @Resource
    private CommonConfigProperty commonConfigProperty;

    @Bean
    public RouterFunction<ServerResponse> redirectRouter() {
        //log.info("{}",commonConfigProperty);
        return RouterFunctions.route()
                .GET("/logout", request -> {
                    String referer = commonConfigProperty.systemFlag;
                    List<String> referers = request.headers().header("referer");
                    if(referers.size()>0){
                        referer = referers.get(0);
                    }
                    //log.info("{}",referer);
                    return ServerResponse.temporaryRedirect(URI.create(commonConfigProperty.systemFlag+"/CAS/logout?client_id="+referer+"&service="+commonConfigProperty.systemFlag+"/logout"))
                            .build();
                })
                .build();
    }
}
